/**
 * 
 */
package com.yidu.erp.action.BeyondRoleModel;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yidu.erp.model.BeyondRoleModel;
import com.yidu.common.SsmMessage;
import com.yidu.erp.service.BeyondRoleModel.RoleModelService;

/**
 * 角色模块action
 * @author wws
 * @date  2017年11月11日
 */
@Controller
@RequestMapping("rolemodel")
public class BeyondRoleModelAction {
	
	@Resource
	private RoleModelService roleModelService;
	
	SsmMessage message = new SsmMessage();
	
	/**
	 * 增加角色模块
	 * @param rolemodel
	 * @return
	 */
	@RequestMapping("/saverolemodel")
	@ResponseBody
	public SsmMessage saverolemodel(BeyondRoleModel rolemodel){
		System.out.println("进来了BeyondRoleModelAction增加角色模块方法，角色模块id="+rolemodel.getRoleModelId()+"模块id="+rolemodel.getModelId()+"角色id="+rolemodel.getRoleId());
		SsmMessage message = new SsmMessage();
		
		int rows = roleModelService.insertSelective(rolemodel);
		System.out.println("增加角色模块成功，rows="+rows);
		//int sc = personelRoleService.deleteByPrimaryKey(personelrole.getStaffId());
		if(rows>0){
			message.setState(SsmMessage.SUCCESS);
			message.setMes(SsmMessage.SUCCESS_MES);
		}else{
			message.setState(SsmMessage.ERROR);
			message.setMes(SsmMessage.ERROR_MES);
		}
		return message;
		
	}
	
	/**
	 * 增加角色模块
	 * @param moduleid 参数模块ID
	 * @param roleid 参数角色ID
	 * @return 返回int
	 * @author wws
	 * @date 2017年10月31日
	 */
	@RequestMapping("/saverolemodelTwo")
	@ResponseBody
	public SsmMessage saverolemodelTwo(String moduleid,String roleId){
		
		System.out.println("进来了BeyondRoleModelAction增加角色模块Two方法,角色id==="+roleId);
		System.out.println("模块id==="+moduleid+"角色id==="+roleId);
		BeyondRoleModel rm = new BeyondRoleModel();
		Map<String, Object> map = new HashMap<>();
		String[] fgmoduleid = moduleid.split("_");
		
		int fls = 0;
		int has = roleModelService.deleteByPrimaryKey(roleId);
	
			for (int i = 0; i < fgmoduleid.length; i++) {
				String uuid = UUID.randomUUID().toString();
				String rolemoduleid = "rd"+uuid;
				rm.setModelId(fgmoduleid[i]);
				rm.setRoleId(roleId);
				rm.setRoleModelId(rolemoduleid);
				
				fls = roleModelService.insertSelective(rm);
			}
		
		if(fls>0){
			message.setState(SsmMessage.SUCCESS);
			message.setMes(SsmMessage.SUCCESS_MES);
		}else{
			message.setState(SsmMessage.ERROR);
			message.setMes(SsmMessage.ERROR_MES);
		}
	
		return message;
		
	}
	
	
	/**
	 * 根据角色id删除角色模块信息
	 * @param request
	 * @return
	 */
	@RequestMapping("/delrolemodule")
	@ResponseBody
	public SsmMessage delrolemodule(HttpServletRequest request){
		String id = request.getParameter("roleId");
		System.out.println("进来了BeyondRoleModelAction根据角色id删除角色模块信息方法,id==="+id);
		int rosw = roleModelService.deleteByPrimaryKey(id);
		if(rosw>0){
			message.setState(SsmMessage.SUCCESS);
			message.setMes(SsmMessage.SUCCESS_MES);
		}else{
			message.setState(SsmMessage.SUCCESS);
			message.setMes(SsmMessage.SUCCESS_MES);
		}
		return message;
		
	}
}
